package com.example.safeassistan.train.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.RequiresApi;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import com.cy.tablayoutniubility.FragPageAdapterVp2;
import com.cy.tablayoutniubility.TabAdapter;
import com.cy.tablayoutniubility.TabLayoutScroll;
import com.cy.tablayoutniubility.TabMediatorVp2;
import com.cy.tablayoutniubility.TabViewHolder;
import com.example.safeassistan.R;
import com.example.safeassistan.base.BaseActivity;
import com.example.safeassistan.constants.UrlConstants;
import com.example.safeassistan.http.HttpRequest;
import com.example.safeassistan.http.HttpStringCallBack;
import com.example.safeassistan.risk.model.RiskPointTableDetailModel;
import com.example.safeassistan.train.fragment.MaterialFragment;
import com.example.safeassistan.train.model.CloudCategory;
import com.example.safeassistan.utile.JsonUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.example.safeassistan.common.MethodUtils.getUserId;


/***资料云库**/
public class ActivityKnowledgeBase extends BaseActivity {

    private TabLayoutScroll toptab1;
    //资料云库头部分类
    private CloudCategory cloudCategory;
    /**View**/
    private ViewPager2 viewpager;

    public static void actionStart(Context context) {
        Intent intent = new Intent(context, ActivityKnowledgeBase.class);
        context.startActivity(intent);
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        setAndroidNativeLightStatusBar(true);
    }

    @Override
    protected int setLayoutResourceID() {
        return R.layout.activity_knowledge_base;
    }

    @Override
    protected void initView() {
        setTitleText("知识库");
        toptab1=findViewById(R.id.toptab1);
        viewpager=findViewById(R.id.viewpager);
        getDataType();
    }

    //获取资料云库分类
    public void getDataType(){
        new HttpRequest(getContext()).doGet(UrlConstants.taskquerySectionList, "", new HttpStringCallBack() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onSuccess(Object result) {
                String datas = result.toString();
                JSONObject jsonObject = null;
                try {
                    jsonObject = new JSONObject(datas);
                    String message = jsonObject.getString("message");
                    Boolean success = jsonObject.getBoolean("success");
                    if (success) {
                        Object obj = JsonUtils.fromJson(result.toString(), CloudCategory.class);
                        if (obj instanceof CloudCategory) {
                            cloudCategory=(CloudCategory)obj;
                            if (cloudCategory.getResult().size()>0){
                                setTabItem();
                                toptab1.setVisibility(View.VISIBLE);
                            }else {
                                toptab1.setVisibility(View.GONE);
                            }
                        }
                    }else {
                        Toast.makeText(ActivityKnowledgeBase.this,message,Toast.LENGTH_SHORT).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int code, String msg) { }
        });
    }

    //设置头部筛选器item
    public void setTabItem(){
        FragPageAdapterVp2<String> fragmentPageAdapter = new FragPageAdapterVp2<String>(this) {

            @Override
            public Fragment createFragment(String bean, int position) {
                MaterialFragment materialFragment= MaterialFragment.newInstance(cloudCategory.getResult().get(position).getId());
                return materialFragment;
            }

            @Override
            public void bindDataToTab(TabViewHolder holder, int position, String bean, boolean isSelected) {
                TextView textView = holder.getView(R.id.tv);
                if (isSelected) {
                    textView.setTextColor(0xff3B5BEA);
                    textView.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                } else {
                    textView.setTextColor(0xff444444);
                    textView.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
                }
                textView.setText(bean);
            }

            @Override
            public int getTabLayoutID(int position, String bean) {
                return R.layout.tab_item;
            }
        };
        TabAdapter<String> tabAdapter = new TabMediatorVp2<String>(toptab1, viewpager).setAdapter(fragmentPageAdapter);

        List<String> list = new ArrayList<>();
        for(int i=0;i<cloudCategory.getResult().size();i++){
            list.add(cloudCategory.getResult().get(i).getName());
        }
        fragmentPageAdapter.add(list);
        tabAdapter.add(list);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected int getTitleBarType() {
        return TITLEBAR_DEFAULT;
    }
}
